// 配置ajax
$.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
	});
var grade,pid,pelement,element,id,pid,classname,ctime;
// 捕获父元素
function fun(me) {
	$('#my_create').fadeIn();
	if($(me).attr('grade') == 0){
		$('#my_reminder').text('创建新的一级分类');
		grade = 0;
		pid = $(me).attr('pid');
		pelement = $('tr').first();
		
	}else if($(me).attr('grade') == 1){

		$('#my_reminder').text('创建新的二级分类');
		grade = 1;
		pid = $(me).attr('pid');
		pelement = $(me).parent().parent();

		
	}else if($(me).attr('grade') == 2){
		$('#my_reminder').text('创建新的三级分类');
		grade = 2;
		pid = $(me).attr('pid');
		pelement = $(me).parent().parent();
		

	}else{
		alert('未知错误 请联系管理员');
		return;
	}
}
// 获取想要创建的分区和pid
// 用ajax将数据发送到执行添加
// 发送ajax的方法
function ajax(pid,val,grade){
	$.ajax({
		url:'/admin/classified/insert',
		type:'post',
		dataType:'json',
		async:false,
		data:{'pid':pid,'val':val,'grade':grade},
		success:function(res){
			if(parseInt(res) == 0) {
				alert('创建失败 请重试');
				return;
			}
			// console.log(pelement);

			id = res.id;
			pid = res.pid;
			classname = res.classname;
			ctime = res.ctime;
			dograde(grade);
			pelement.after(element);
			
		}
	})
}
// 取消创建按钮
$('[info=my_button]:eq(1)').click(function(){
	$('#my_create').fadeOut();
});
// 确认创建按钮
$('[info=my_button]:eq(0)').click(function(){

	$('#my_create').fadeOut(function(){
		// 判断分类的名字是否为空
		if($('#my_classified').val().trim() == ''){
			alert('分类名不能为空');
			return;
		}
		// 调用方法发送ajax
		ajax(pid,$('#my_classified').val(),grade);
	});
});
function dograde(grade) {
	switch (grade) {
		case 0: element = "<tr class='gradeA odd active'><td >"+classname+"</td> <td >"+ctime+"</td><td class='center'>一级分类</td>  <td class='center ''><a class='btn-floating black' style='transform: scaleY(1) scaleX(1) translateY(0px) translateX(0px); opacity: 1;'><i class='material-icons'>insert_chart</i></a></td><td class='center' ><a class='btn disabled' ><i class='material-icons'>publish</i></a></td> </tr>";

		break;
		case 1: element = '<tr class="gradeA odd active"><td class=" ">'+classname+'</td><td class=" ">'+ctime+'</td><td class="center ">二级分类</td><td class="center "><a class="btn-floating black" style="transform: scaleY(1) scaleX(1) translateY(0px) translateX(0px); opacity: 1;"><i class="material-icons">insert_chart</i></a></td><td class="center "><a class="btn disabled" ><i class="material-icons">publish</i></a></td></tr>';

		break;
		case 2: element = '<tr class="gradeA odd active"><td class=" ">'+classname+'</td><td class=" ">'+ctime+'</td><td class="center ">三级分类</td> <td class="center "><a class="btn-floating black" style="transform: scaleY(1) scaleX(1) translateY(0px) translateX(0px); opacity: 1;"><i class="material-icons">insert_chart</i></a></td><td class="center "><a class="btn disabled" ><i class="material-icons">publish</i></a></td> </tr>';

		break
	}
}